package cn.yangliu.nacos.oauth2.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashSet;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import cn.yangliu.nacos.comm.audit.AbstractEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * The interface User.
 *
 * @author 问道于盲
 * @date 2019 -03-20 00:08
 */
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@Getter
@NoArgsConstructor
@Setter
@Table(name = "oauth2_user")
@ToString
@Entity
public class User extends AbstractEntity implements Serializable, UserDetails {

    /**
     * current class's static member
     * The constant serialVersionUID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * current class instance's member.
     * The Username.
     */
    @Column(updatable = false, unique = true)
    private String username;

    /**
     * current class instance's member.
     * The Password.
     */
    @Column(updatable = false)
    private String password;

    /**
     * current class instance's member.
     * The Email.
     */
    private String email;

    /**
     * current class instance's member.
     * The Mobile.
     */
    private String mobile;

    /**
     * current class instance's member.
     * The Account expire time.
     */
    private LocalDateTime accountExpireTime = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);

    /**
     * current class instance's member.
     * The Enable.
     */
    private Boolean enable = true;

    /**
     * current class instance's member.
     * The Last password update time.
     */
    private LocalDateTime lastPasswordUpdateTime = LocalDateTime.now();

    /**
     * current class method for getter.
     * Gets authorities.
     *
     * @return the authorities
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new HashSet<>();
    }

    /**
     * Is account non expired boolean.
     *
     * @return the boolean
     */
    @Override
    public boolean isAccountNonExpired() {
        LocalDateTime now = LocalDateTime.now();
        return accountExpireTime.isAfter(now);
    }

    /**
     * Is account non locked boolean.
     *
     * @return the boolean
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * Is credentials non expired boolean.
     *
     * @return the boolean
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * Is enabled boolean.
     *
     * @return the boolean
     */
    @Override
    public boolean isEnabled() {
        return enable;
    }


}
